﻿using System.Collections.Generic;
using Assets.Scripts.Net;
using Assets.Scripts.Net.Data;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.Net.Loader;
using UnityEngine;
using UnityEngine.UI;
using Debug = UnityEngine.Debug;

namespace Assets.Scripts.UI.Panel
{
    public enum UserType
    {
        Student,
        Teacher
    }

    public class MainPanel : UIBase<MainPanel>
    {
        #region 组件
        public Button UserInfoButton;//用户信息
        public Button HomeWorkButton;//家庭作业
        public Button EnterExamButton;//进入考场
        public Button MockButton;//模拟考
        public Button GetScoreButton;//得到分数
        public Button ExitButton;//退出
        public Button SettingButton;//系统设置
        public Button CollectBookButton;//收藏夹
        #endregion

        protected override UIType UIType
        {
            get { return UIType.Panel; }
        }

        protected override void InitOnStart()   
        {
            AutoFindComponent(transform,true);
            UserInfoButton.onClick.AddListener(OnUserInfoButtonClick);
            HomeWorkButton.onClick.AddListener(OnHomeWorkButtonClick);
            EnterExamButton.onClick.AddListener(OnEnterExamButtonClick);
            MockButton.onClick.AddListener(OnMockButtonClick);
            GetScoreButton.onClick.AddListener(OnGetScoreButtonClick);
            ExitButton.onClick.AddListener(OnExitButtonClick);
            SettingButton.onClick.AddListener(OnSettingButtonClick);
            CollectBookButton.onClick.AddListener(OnCollectedButtonClick);      
        }

        
        /// <summary>
        /// 收藏夹，数据变动性比较大，不缓存数据
        /// </summary>
        private void OnCollectedButtonClick()
        {
            Transform tsf = CreatePanel(PanelType.LoadingLoopPanel);
            tsf.GetComponent<LoadingLoopPanel>().Init();
            Dictionary<string, object> dict = NetManager.Instance.GetDict();
            NetManager.Instance.SendRequest(RequestType.ErrorList, dict, () =>
            {
                List<CourseInfo> courses =
                    NetReaderFactory.Instance().GetInfoByType<List<CourseInfo>>() as
                        List<CourseInfo>;
                List<QueInfo> queInfos = NetReaderFactory.Instance().GetInfoByType<List<QueInfo>>()as List<QueInfo>;
                CollectPanel panel = CreatePanel(PanelType.CollectPanel).GetComponent<CollectPanel>();                
                panel.Init(courses,queInfos);
                Destroy(tsf.gameObject);
            });
        }

        /// <summary>
        /// 设置按钮
        /// </summary>
        private void OnSettingButtonClick()
        {
            CreatePanel(PanelType.SettingPanel);
        }

        private void OnUserInfoButtonClick()
        {
            CreatePanel(PanelType.StudentInfoPanel);  
        }

        /// <summary>
        /// 课后作业
        /// </summary>
        private void OnHomeWorkButtonClick()
        {
            //校验本地信息
            List<CourseInfo> localInfos = NetReaderFactory.Instance().GetInfoByName(NetConstValue.HW_CourseInfo) as List<CourseInfo>;
            if (localInfos != null)
            {
                HomeWorkPanel panel = CreatePanel(PanelType.HomeWorkPanel).GetComponent<HomeWorkPanel>();
                panel.Init(localInfos);
                return;
            }
            Transform tsf = CreatePanel(PanelType.LoadingLoopPanel);
            tsf.GetComponent<LoadingLoopPanel>().Init();
            Dictionary<string, object> dict = NetManager.Instance.GetDict();
            NetManager.Instance.SendRequest(RequestType.HomeWork, dict, () =>
            {
                //这里没有需要加载的资源
                HomeWorkPanel panel = CreatePanel(PanelType.HomeWorkPanel).GetComponent<HomeWorkPanel>();
                List<CourseInfo> infos = NetReaderFactory.Instance().GetInfoByType<List<CourseInfo>>(NetConstValue.HW_CourseInfo) as List<CourseInfo>;
                //Debug.Log("courseinfos.count:"+infos.Count);    
                NetReaderFactory.Instance().GetInfoByType<List<HomeWorkInfo>>(NetConstValue.HW_HomeInfo);
                panel.Init(infos);
                Destroy(tsf.gameObject);
            });
        }

        /// <summary>
        /// 进入考试 
        /// </summary>
        private void OnEnterExamButtonClick()
        {
            //考试的信息变化较大，暂时不考虑，比较本地信息
            Dictionary<string, object> dict = NetManager.Instance.GetDict();
            NetManager.Instance.SendRequest(RequestType.EnterExamination, dict, () =>
            {
                if (NetReaderFactory.Instance().NetResUrls.Count > 0)//有需要加载的资源
                {
                    LoadingPanel loadingPanel= CreatePanel(PanelType.LoadingPanel).GetComponent<LoadingPanel>();
                    loadingPanel.Init(LoadingType.WWWLoading);
                    Global.Global.Instance.StartCoroutine(WWWLoadManager.Instance()
                        .DownLoadSaveResource(NetReaderFactory.Instance().NetResUrls, () =>
                        {
                            Exit("LoadingPanel(Clone)");
                            ExamRulePanel panel = CreatePanel(PanelType.ExamRulePanel).GetComponent<ExamRulePanel>();
                            ExamInfo info = NetReaderFactory.Instance().GetInfoByType<ExamInfo>() as ExamInfo;
                            panel.Init(info);
                        }));
                }
                else//没有需要加载的资源
                {
                    ExamRulePanel panel = CreatePanel(PanelType.ExamRulePanel).GetComponent<ExamRulePanel>();
                    ExamInfo info = NetReaderFactory.Instance().GetInfoByType<ExamInfo>() as ExamInfo;
                    Debug.Log(info.examname);
                    Debug.Log(info.countdown);
                    panel.Init(info);
                }
            }); 
        }

        /// <summary>
        /// 模拟考
        /// </summary>
        private void OnMockButtonClick()
        {
            List<CourseInfo> courseInfos =
                NetReaderFactory.Instance().GetInfoByName(NetConstValue.Mock_CourseInfo) as List<CourseInfo>;
            if (courseInfos != null)
            {
                MockExamPanel panel = CreatePanel(PanelType.MockExamPanel).GetComponent<MockExamPanel>();
                panel.Init(courseInfos);
                return;   
            }           
            Dictionary<string, object> dict = NetManager.Instance.GetDict();
            Transform tsf = CreatePanel(PanelType.LoadingLoopPanel);
            tsf.GetComponent<LoadingLoopPanel>().Init();
            NetManager.Instance.SendRequest(RequestType.MockExam, dict, () =>
            {
                MockExamPanel panel = CreatePanel(PanelType.MockExamPanel).GetComponent<MockExamPanel>();
                courseInfos =
                    NetReaderFactory.Instance().GetInfoByType<List<CourseInfo>>(NetConstValue.Mock_CourseInfo) as
                        List<CourseInfo>;
                NetReaderFactory.Instance().GetInfoByType<List<PaperInfo>>(NetConstValue.Mock_PaperInfo); //转存
                //Debug.Log("paperinfos.count:"+paperInfos.Count);
                //Debug.Log("courseinfos.count:"+courseInfos.Count);
                panel.Init(courseInfos);
                Destroy(tsf.gameObject);
            });
        }

        /// <summary>
        /// 查询分数
        /// </summary>
        private void OnGetScoreButtonClick()
        {
            CreatePanel(PanelType.CheckScorePanel);
        }

        /// <summary>
        /// 退出
        /// </summary>
        private void OnExitButtonClick()
        {
            TipPanel panel = CreatePanel(PanelType.TipPanel).GetComponent<TipPanel>();
            panel.Init("确认退出程序吗？",Application.Quit);
        }
    }
}
